﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.Windows.Markup;

namespace PrintManager
{
    public class LayoutDefinition
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public List<string> Reports { get; set; }
        public List<string> Papers { get; set; }
        public List<LayoutElement> Elements { get; set; }

        public static LayoutDefinition Load(string filename)
        {
            LayoutDefinition pl = new LayoutDefinition();
            pl.Elements = new List<LayoutElement>();
            if (File.Exists(filename))
            {
                StreamReader sr = new StreamReader(filename, Encoding.Default);
                string line;
                StringBuilder sb = new StringBuilder();
                string name = "";
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.StartsWith("#"))
                    {
                        string[] ff = line.Split('=');
                        switch (ff[0].ToUpper())
                        {
                            case "#ID":
                                pl.Id = ff[1];
                                break;
                            case "#NAME":
                                pl.Name = ff[1];
                                break;
                            case "#REPORTS":
                                pl.Reports = ff[1].Split(',').ToList();
                                name = "";
                                break;
                            case "#PAPERS":
                                pl.Papers = ff[1].Split(',').ToList();
                                name = "";
                                break;
                            case "#ELEMENT":
                                if (name != "" && sb != null && sb.Length > 0)
                                {
                                    pl.Elements.Add(new LayoutElement { Id = name, Xaml = sb.ToString() });
                                }
                                name = ff[1];
                                sb.Clear();
                                break;
                        }
                    }
                    else
                        sb.Append(line);
                }
                if (name != "" && sb != null && sb.Length > 0)
                {
                    pl.Elements.Add(new LayoutElement { Id = name, Xaml = sb.ToString() });
                }
                sr.Close();
            }
            return pl;
        }
    }
}
